/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;


import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.expert.dao.CareDao;
import com.chlitina.store.modules.expert.entity.Care;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 系统护理Service
 * @author liujiang
 * @version 2013-11-02
 */
@Component
@Transactional(readOnly = true)
public class CareService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CareService.class);
	
	@Autowired
	private CareDao careDao;
	
	public Care get(Long id) {
		return careDao.findOne(id);
	}
	
	public Page<Care> find(Page<Care> page, String searchName, Care care) {
		Long storeTypeId = StoreUtils.getStore().getStoreType().getId();
		if (!StringUtils.isNotEmpty(searchName)){
			searchName = "";
		}
		String sql = "select c.* from care c JOIN care_store_type cst ON c.NAME like '%"+ 
				searchName +"%' AND cst.STORE_TYPE_ID = " + storeTypeId + " AND c.ID = cst.CARE_ID";
		return careDao.findBySql(page, sql);
	}
	
	@Transactional(readOnly = false)
	public void save(Care care) {
		careDao.save(care);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		careDao.delete(id);
	}
	
	//获取当前记录排第几位
	public int getIndexById(Long currentId, String name) {
		DetachedCriteria dc = careDao.createDetachedCriteria();
		dc.add(Restrictions.le("id", currentId));
		if (StringUtils.isNotEmpty(name)){
			dc.add(Restrictions.like("name", "%"+ name +"%"));
		}
		dc.addOrder(Order.asc("id"));
		return (int)careDao.count(dc);
	}
}
